<template>
  <div class="community">
    <div class="top">
      <el-descriptions title="社区频道">
      </el-descriptions>
    </div>

    <div class="infinite-list-wrapper">
      <ul
        v-infinite-scroll="load"
        class="list"
        :infinite-scroll-disabled="disabled"
      >
        <li
          v-for="i in tags"
          :key="i"
          class="list-item"
          @mouseover="emitThis(i)"
          @click="showArticleByTag(i)"
        >
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: flex-start;
            "
          >
            <div
              style="
                width: 100px;
                height: 100px;
                border-radius: 10%;
                overflow: hidden;
                margin: 20px 10px 20px 30px;
              "
            >
              <img
                style="width: 100%; height: 100%; object-fit: cover"
                :src="i.picture"
                alt=""
              />
            </div>
            <div
              style="
                font-size: 14px;
                font-size: x-large;
                font-weight: bold;
                width: 85%;
              "
            >
              <el-descriptions :title="i.name" style="margin-left: 10px;">
                <el-descriptions-item label="">
                  <el-text line-clamp="2">
                    {{ i.description }}
                  </el-text>
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </div>
          <div style="flex: auto;text-align: right;margin:0px 10px;align-self: center;">
              <el-button type="info" :icon="i.relation?'StarFilled':'Star'" :plain="true" @click.stop="star(i)">{{ i.relation?"Unstar":"Star" }}</el-button>
          </div>
        </li>
      </ul>
      <p v-if="loading">Loading...</p>
      <p v-if="noMore">No more</p>
    </div>
  </div>
</template>
 
<script>
import { computed } from "vue";
import axios from "axios";
import { ElMessage } from 'element-plus'

export default {
  name: "Community",
  components: {},
  props: {},
  data() {
    return {
      tags:[],
      count: 1,
      loading: false,
      noMore: computed(() => {
        return this.count >= this.tags.length;
      }),
      disabled: computed(() => {
        return this.loading || this.noMore;
      }),
    };
  },
  computed: {},
  watch: {},
  methods: {
    load() {
      this.loading = true;
      setTimeout(() => {
        this.count += 2;
        this.loading = false;
      }, 500);
    },
    emitThis(i) {
      this.$emit("emitAsideRight", i);
    },
    showArticleByTag(i){
      this.$emit("showArticleByTag",i)
    },
    star(item){
      console.log(item);
      axios.get("http://localhost:8888/tag/star",{
        params:{
          t_id:item.t_id,
          u_id:this.$store.state.user.u_id
        }
      }).then(data=>{
        if(data.data.data){
          ElMessage({
            message: "收藏成功",
            type: 'success',
          })
        }else{
          ElMessage({
            message: data.data.msg,
            type: 'warning',
          })
        }
        this.$router.go(0)
      })
    },
    init(){
      axios.get("http://localhost:8888/tag/init",{
        params:{
          u_id:this.$store.state.user.u_id
        }
      }).then(data=>{
        if(data.data.data){
          this.tags = data.data.data
        }
      })
    }
  },
  created() {
    this.$store.state.lastActiveComponent = "community"
    this.init()
  },
  mounted() {},
};
</script>

<style scoped>
.community {
  max-width: 1200px;
  margin: 0 auto;
  /* 高度没有设置会导致无限触发首次填满 */
  height: 100%; 
}

.top {
  background-color: #f5f5f5 !important;
  padding: 20px;
}

/* li内的 样式 */
.list-item {
  display: flex;
  margin-bottom: 20px;
  cursor: pointer;
}

.list-item img {
  width: 70px;
  height: 70px;
  border-radius: 10%;
  object-fit: cover;
}

.list-item .content {
  flex: 1;
  margin-left: 10px;
}

.list-item .content .title {
  font-size: 14px;
  font-size: x-large;
  font-weight: bold;
}

.list-item .content .description {
  font-size: 12px;
  color: #999aaa;
}

.infinite-list-wrapper {
  height: 100%;
}

.list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.list li {
  position: relative;
  padding: 10px;
  border-bottom: 1px solid #e5e5e5;
}

.list li:last-child {
  border-bottom: none;
}

.list :hover {
  background-color: #f5f5f5 !important;
}
</style>